//app.js
var core = require("utils/core.js");
App({
  onShow: function() {
    this.onLaunch();
  },
  onLaunch: function() {


  },
  getUserInfo: function(t, i) {
    console.log('getUserInfo方法');
    var that = this;
    var o = this
    var n = null
    wx.login({
      success: function(a) {
        a.code ? core.post("wxapp/login", {
          code: a.code
        }, function(a) {
          // console.log("login返回值：", JSON.stringify(a))
          a.error ? core.alert("获取用户登录态失败:" + a.message) : a.isclose && i && "function" == typeof i ? i(a.closetext, !0) : wx.getUserInfo({ //在用户未授权过的情况下调用此接口，将不再出现授权弹窗，会直接进入 fail 回调（详见《公告》)。在用户已授权的情况下调用此接口，可成功获取用户信息
            success: function(i) {
              wx.setStorage({
                key: 'sessionKey',
                data: a.session_key,
              })
              // console.log('获取的信息：：--》'+JSON.stringify(i));
              n = i.userInfo, core.get("wxapp/auth", {
                data: i.encryptedData,
                iv: i.iv,
                sessionKey: a.session_key,
                is_group: 1
              }, function(e) {
                console.log('=========' + JSON.stringify(e))
                // console.log(i)
                i.userInfo.openid = e.openId, i.userInfo.id = e.id, i.userInfo.uniacid = e.uniacid,
                  i.needauth = 0, that.setCache("userinfo", i.userInfo, 365 * 24 * 60 * 60), that.setCache("userinfo_openid", i.userInfo.openid),
                  o.setCache("userinfo_id", e.id), o.getSet(), t && "function" == typeof t && t(n);
              });
            },
            fail: function() { //在用户未授权过的情况下调用此接口，将不再出现授权弹窗，会直接进入 fail 回调
              wx.navigateTo({
                url: '/pages/login/login',
              })
              // e.get("wxapp/check", {
              //   openid: a.openid
              // }, function (e) {
              //   e.needauth = 1, o.setCache("userinfo", e, 7200), o.setCache("userinfo_openid", a.openid),
              //     o.setCache("userinfo_id", a.id), o.getSet(), t && "function" == typeof t && t(n);
              // });
            }
          });
        }) : core.alert("获取用户登录态失败:" + a.errMsg);
      },
      fail: function() {
        core.alert("获取用户信息失败!");
      }
    })
  },
  getSet: function() {
    var t = this,
      i = t.getCache("cacheset");
    "" == i && setTimeout(function() {
      core.get("cacheset", {
        version: i.version
      }, function(e) {
        console.log(e), e.update && t.setCache("cacheset", e.data);
      });
    }, 10);
  },
  getCache: function(e, t) {
    var i = +new Date() / 1e3,
      o = "";
    i = parseInt(i);
    try {
      (o = wx.getStorageSync(e + this.globalData.appid)).expire > i || 0 == o.expire ? o = o.value : (o = "",
        this.removeCache(e));
    } catch (e) {
      o = void 0 === t ? "" : t;
    }
    return o = o || "";
  },
  setCache: function(e, t, i) {
    var o = +new Date() / 1e3,
      n = !0,
      a = {
        expire: i ? o + parseInt(i) : 0,
        value: t
      };
    try {
      wx.setStorageSync(e + this.globalData.appid, a);
    } catch (e) {
      n = !1;
    }
    return n;
  },
  removeCache: function(e) {
    var t = !0;
    try {
      wx.removeStorageSync(e + this.globalData.appid);
    } catch (e) {
      t = !1;
    }
    return t;
  },
  getConfig: function() {
    if (null !== this.globalData.api) return {
      api: this.globalData.api,
      approot: this.globalData.approot,
      appid: this.globalData.appid
    };
    var e = wx.getExtConfigSync();
    return console.log(e), this.globalData.api = e.config.api, this.globalData.approot = e.config.approot,
      this.globalData.appid = e.config.appid, e.config;
  },
  globalData: {
    userInfo: null,
    // appid: "wx2ea634acd98f7c53",
    // api: "https://shop.haizhongcheng.com/app/ewei_shopv2_api.php?i=5",
    // approot: "https://shop.haizhongcheng.com/addons/ewei_shopv2/",

  // 湛江海中城
    appid: "wxebed2939ce341e4f",
    api: "https://it-rrshop.haizhongcheng.com/app/ewei_shopv2_api.php?i=5",
    approot: "https://it-rrshop.haizhongcheng.com/addons/ewei_shopv2/",


      //     //UAT// 广州海中城
      // appid: "wx3c6159cfe23da818",
      // api: "https://it-uatshop.haizhongcheng.com/app/ewei_shopv2_api.php?i=5",
      // approot: "https://it-uatshop.haizhongcheng.com/addons/ewei_shopv2/",

    //酷奇小店
    // appid: "wx16d7b617f8bd1e85",
    // api: "https://it-rrshop.haizhongcheng.com/app/ewei_shopv2_api.php?i=5",
    // approot: "https://it-rrshop.haizhongcheng.com/addons/ewei_shopv2/",
  }
})